﻿using Microsoft.Extensions.DependencyInjection;
using Sino.CapacityCloud.Repositories.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Sino.CapacityCloud.Services
{

    public class TransportLinkageManager
    {

        private readonly IServiceProvider _serviceProvider;

        public TransportLinkageManager(IServiceProvider serviceProvider)
        {
            this._serviceProvider = serviceProvider;
        }

        public TransportLinkage AddOrUpdateTransportLinkage(TransportLinkage line)
        {
            using (var serviceScope = this._serviceProvider.GetRequiredService<IServiceScopeFactory>().CreateScope())
            {
                using (var dbContext = serviceScope.ServiceProvider.GetService<ApplicationDbContext>())
                {
                    var findEntity = dbContext.TransportLinkages.FirstOrDefault(x => x.Equals(line));
                    if (findEntity == null)
                    {
                        dbContext.TransportLinkages.Add(line);
                        dbContext.SaveChanges();
                        return line;
                    }
                    return findEntity;
                }
            }
        }

        public TransportLinkage AddOrUpdateTransportLinkage(ApplicationUser user, TransportLinkage line, bool isCommonLine = true)
        {
            using (var serviceScope = this._serviceProvider.GetRequiredService<IServiceScopeFactory>().CreateScope())
            {
                using (var dbContext = serviceScope.ServiceProvider.GetService<ApplicationDbContext>())
                {
                    var findEntity = dbContext.TransportLinkages.FirstOrDefault(x => x.Equals(line));
                    if (findEntity == null)
                    {
                        dbContext.TransportLinkages.Add(line);
                        dbContext.SaveChanges();
                        findEntity = line;
                    }
                    if (isCommonLine)
                    {
                        if (!user.CommonTransportLinkages.Any(x => x.TransportLinkageId == findEntity.TransportLinkageId))
                        {
                            dbContext.CommonTransportLinkages.Add(new CommonTransportLinkage()
                            {
                                TransportLinkageId = findEntity.TransportLinkageId,
                                OperationTime = DateTime.Now,
                                UserId = user.Id
                            });
                            dbContext.SaveChanges();
                        }
                    }
                    else
                    {
                        if (!user.FavouriteTransportLinkages.Any(x => x.TransportLinkageId == findEntity.TransportLinkageId))
                        {
                            dbContext.FavouriteTransportLinkages.Add(new FavouriteTransportLinkage()
                            {
                                TransportLinkageId = findEntity.TransportLinkageId,
                                OperationTime = DateTime.Now,
                                UserId = user.Id
                            });
                            dbContext.SaveChanges();
                        }
                    }
                    return findEntity;
                }
            }
        }

    }

}